import 'package:casdon/core/utils/string_externtion.dart';
import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';
import 'package:json_annotation/json_annotation.dart';

part 'recipe_module_content_api.g.dart';

// 请求参数模型
@JsonSerializable()
class RequestRecipeModuleContentApi {
  final String moduleId;
  final PageQuery pageQuery;

  RequestRecipeModuleContentApi({
    required this.moduleId,
    required this.pageQuery,
  });

  factory RequestRecipeModuleContentApi.fromJson(Map<String, dynamic> json) =>
      _$RequestRecipeModuleContentApiFromJson(json);

  Map<String, dynamic> toJson() {
    final jsonMap = _$RequestRecipeModuleContentApiToJson(this);
    jsonMap['pageQuery'] = pageQuery.toJson(); // 手动确保pageQuery被转换成Map
    return jsonMap;
  }
}

// 分页查询参数
@JsonSerializable()
class PageQuery {
  final int pageNum;
  final int pageSize;
  final String isAsc;
  final String orderByColumn;

  PageQuery({
    required this.pageNum,
    required this.pageSize,
    this.isAsc = 'asc',
    this.orderByColumn = 'sort',
  });

  factory PageQuery.fromJson(Map<String, dynamic> json) =>
      _$PageQueryFromJson(json);

  Map<String, dynamic> toJson() => _$PageQueryToJson(this);
}

// 响应模型
@JsonSerializable()
class ResponseRecipeModuleContentApi {
  final int total;
  final List<RecipeModuleContent> rows;

  ResponseRecipeModuleContentApi(this.total, this.rows);

  factory ResponseRecipeModuleContentApi.fromJson(Map<String, dynamic> json) =>
      _$ResponseRecipeModuleContentApiFromJson(json);

  Map<String, dynamic> toJson() => _$ResponseRecipeModuleContentApiToJson(this);
}

// 菜谱内容模型
@JsonSerializable()
class RecipeModuleContent {
  @JsonKey(name: 'ossUrls', fromJson: _getImageUrl, toJson: _imageUrlToJson)
  final String? imageUrl; // 封面图
  @JsonKey(
    name: 'videoCoverUrls',
    fromJson: _getVideoCoverUrl,
    toJson: _videoCoverUrlToJson,
  )
  final String? videoCoverUrl; // 视频封面图
  final String? name; // 名称
  final String? url; // 活动链接

  final String id; // ID
  final String? recipeId; // recipeID
  final int? isControl; // 是否是智能菜谱，一周菜谱会用
  final String? view;
  final String? collect;
  final String? count; // 菜谱集时有意义：菜谱数量。菜谱时为null，
  final int? sort; // 排序
  RecipeModuleContent({
    this.imageUrl,
    this.videoCoverUrl,
    this.name,
    this.url,
    required this.id,
    required this.recipeId,
    required this.isControl,
    required this.view,
    required this.collect,
    required this.count,
    required this.sort,
  });

  factory RecipeModuleContent.fromJson(Map<String, dynamic> json) =>
      _$RecipeModuleContentFromJson(json);

  Map<String, dynamic> toJson() => _$RecipeModuleContentToJson(this);

  // 自定义转换函数
  static String? _getImageUrl(dynamic value) {
    if (value is String && value.isNotEmpty) {
      return value.split(",").first;
    }
    return null;
  }

  // 反向序列化方法
  static dynamic _imageUrlToJson(String? url) => url;

  // 自定义转换函数
  static String? _getVideoCoverUrl(dynamic value) {
    if (value is String && value.isNotEmpty) {
      return value.split(",").first;
    }

    return null;
  }

  // 反向序列化方法
  static dynamic _videoCoverUrlToJson(String? url) => url;
}
